﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SPServices.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.ComponentModel;
using Microsoft.Practices.SharePoint.Common.Logging;
using Smart.Common.Forms;
using Smart.Common.Locator;
using Smart.Common.Notifications;
using Smart.Common.Router;
using Smart.Common.Rules;
using Smart.Common.Security;
using Smart.Common.Templates;

//using Smart.Common.Templates;

namespace Smart.Common
{
	/// <summary>
	/// Service context 
	/// </summary>
	[Localizable(false)]
	public class SPServices
	{
		/// <summary>
		/// Service context 
		/// </summary>
		static SPServices()
		{
		}

		public static SyncDictionary<Guid, SPServicesInstance> Instances = new SyncDictionary<Guid, SPServicesInstance>();

		public static SPServicesInstance Current
		{
			get
			{
				var site = SPH.GetSite();

				if (site == null)
					throw new InvalidOperationException("Site is not defined!");

				var instance = Instances[site.ID, siteId => new SPServicesInstance(siteId)];

				instance.Load();

				return instance;
			}
		}

		/// <summary>
		/// Main configuration service
		/// </summary>
		public static IConfiguration Configuration { get { return Current.Configuration; } }

		/// <summary>
		/// Main service locator
		/// </summary>
		public static IServiceLocator ServiceLocator { get { return Current.ServiceLocator; } }

		/// <summary>
		/// SharePoint logger (Trace write to ULS, Log write to EventLog)
		/// </summary>
		public static ILogger Logger { get { return Current.Logger; } }

		/// <summary>
		/// Add-on manager
		/// </summary>
		public static IAddonManager AddonManager { get { return Current.AddonManager; } }

		/// <summary>
		/// Event manager
		/// </summary>
		public static IEventManager EventManager { get { return Current.EventManager; } }

		/// <summary>
		/// Template factory
		/// </summary>
		public static ITemplateFactory TemplateFactory { get { return Current.TemplateFactory; } }

		/// <summary>
		/// Rules factory
		/// </summary>
		public static IRulesFactory RulesFactory { get { return Current.RulesFactory; } }

		/// <summary>
		/// Forms factory
		/// </summary>
		public static IFormsFactory FormsFactory { get { return Current.FormsFactory; } }

		/// <summary>
		/// Security factory
		/// </summary>
		public static ISecurityFactory SecurityFactory { get { return Current.SecurityFactory; } }

		/// <summary>
		/// Router factory
		/// </summary>
		public static IRouterFactory RouterFactory { get { return Current.RouterFactory; } }

		/// <summary>
		/// All cache storage
		/// </summary>
		public static readonly ICacheFactory Cache = new CacheFactory();

		/// <summary>
		/// Notifications scope, WARNING! Shoud be disposed!
		/// </summary>
		public static INotificationScope Notifications { get { return Current.Notifications; } }

		/// <summary>
		/// Creates scope context by <paramref name="scopeName"/>
		/// </summary>
		/// <typeparam name="TScopeType">Scope context type</typeparam>
		/// <param name="scopeName">Scope name</param>
		/// <returns>Scope context</returns>
		public static TScopeType GetScope<TScopeType>(string scopeName)
			where TScopeType : class, IEntityScope
		{
			return EntityScope.GetScope<TScopeType>(Current, scopeName);
		}

		/// <summary>
		/// Creates scope context by <paramref name="scopeName"/> with dynamic settings
		/// </summary>
		/// <typeparam name="TScopeType">Scope context Type</typeparam>
		/// <param name="scopeName">Scope name</param>
		/// <param name="scopeSettings">Scope settings</param>
		/// <returns>Scope context</returns>
		public static TScopeType GetScope<TScopeType>(string scopeName, string[] scopeSettings)
			where TScopeType : class, IEntityScope
		{
			return EntityScope.GetScope<TScopeType>(Current, scopeName, scopeSettings);
		}
	}
}